package com.eleadmin.damon.utils.utils;
/************************************************
 MD5 算法的Java Bean
 @author:Topcat Tuppin
 Last Modified:10,Mar,2001
 *************************************************/

import java.security.MessageDigest;

/*************************************************
 md5 类实现了RSA Data Security, Inc.在提交给IETF
 的RFC1321中的MD5 message-digest 算法。
 *************************************************/

public class MD5Utils {
    /***

     * MD5加密 生成32位md5码

     *

     * @return 返回32位md5码

     */

    public static String md5Encode(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 加密
     */

    public static String convertMD5(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }

    /**

     * 解密算法 (执行一次加密)进行两次解密
     * @param str
     * @return
     */
    public static String md5Decode(String str) {
        return convertMD5(convertMD5(str));
    }

    // 测试主函数
    public static void main(String args[]) throws Exception {
        String s = new String("18250555233");
        System.out.println("原始：" + s);
//        System.out.println("MD5后：" + string2MD5(s));
        System.out.println("加密的：" + convertMD5(s));
        System.out.println("解密的：" + convertMD5(convertMD5(s)));
        String t = md5Encode(s);
        System.out.println(t);
        t = string2MD5(t);
        System.out.println(t);
    }

}